package com.eliza.music.view.music.search

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextIndent
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.eliza.music.compoment.music.SongView
import com.eliza.music.model.tmp.TagsList
import com.eliza.music.model.tmp.TempSongLists
import com.eliza.music.ui.theme.PaddingSize

@Preview(showBackground = true, showSystemUi = true)
@Composable
private fun Preview() {
    Search.Show()
}

object Search {

    @Composable
    fun Show(modifier: Modifier = Modifier) {
        val remember = remember {
            mutableStateOf("")
        }
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.White.copy(0.89f))
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.White)
                    .clip(RoundedCornerShape(20.dp)),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextField(
                    value = remember.value, onValueChange = { str ->
                        remember.value = str
                    },
                    modifier = Modifier
                        .height(60.dp)
                        .defaultMinSize(minHeight = 40.dp),
                    prefix = {
                        Icon(Icons.Rounded.Search, contentDescription = "", tint = Color.Gray)
                    },
                    placeholder = {
                        Text(text = "search")
                    },
                    textStyle = TextStyle(
                        color = Color.Gray,
                        fontSize = 16.sp,
                        textAlign = TextAlign.Start,
                        textIndent = TextIndent(2.sp)
                    ),
                    singleLine = true,
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
                    colors = TextFieldDefaults.colors(
                        cursorColor = Color.Black,
                        unfocusedContainerColor = Color.Transparent,
                        focusedContainerColor = Color.Transparent
                    )
                )
                TextButton(onClick = { /*TODO*/ }) {
                    Text(text = "搜索")
                }
            }

            History()
            HotSearchItem()
        }
    }

    @Composable
    fun History(modifier: Modifier = Modifier) {
        LazyHorizontalGrid(
            rows = GridCells.Adaptive(20.dp),
            modifier = Modifier
                .fillMaxWidth()
                .height(90.dp)
                .padding(PaddingSize.Normal.size),
            verticalArrangement = Arrangement.spacedBy(PaddingSize.Normal.size),
        ) {
            TagsList.forEach {
                item {
                    Text(
                        text = it, color = Color.Gray, fontSize = 14.sp,
                        modifier = Modifier
                            .background(Color.White, RoundedCornerShape(PaddingSize.Normal.size))
                            .padding(PaddingSize.Normal.size)
                    )
                }
            }
        }
    }
}

@Composable
fun HotSearchItem(modifier: Modifier = Modifier) {
    Column(
        modifier = Modifier
            .fillMaxWidth(0.7f)
            .padding(PaddingSize.Normal.size)
            .clip(
                RoundedCornerShape(PaddingSize.Normal.size)
            )
            .background(Color.White)

    ) {

        Text(
            modifier = Modifier.padding(PaddingSize.Normal.size),
            text = "热搜榜", fontWeight = FontWeight.W400,
            fontSize = 20.sp
        )
        TempSongLists.subList(0, 20).forEachIndexed() { index, it ->
            SongView.SongItemInSearch(index, Song = it)
        }
    }

}
